const Article = require('../model/article');
const Tab = require('../model/tab');
/**
 * 文章中间件
 * 
 */
module.exports = {
    /**
     * 获取热门文章
     */
    getHot:(req,res,next) =>{
        Article.getHot(3).then(results =>{
            req.hots = results;
            next()
        }).catch(err=>{
            next(err);
        });
    },
    /**
     * 获取最新文章
     */
    getList:(req,res,next) =>{
        let {start,size} = req.page
        Article.getList(start,size).then(results =>{
            req.article = results;
            next()
        }).catch(err=>{
            next(err);
        });
    },
    /**
     * 获取指定文章列表下的文章总数
    */
    getCategoryCount:(req,res,next) =>{
        // 从路由中传递参数进来
        let id = req.params.id;
        Article.getCategoryCount(id).then(results =>{
            req.articleCount = results;
            next()
        }).catch(err=>{
            next(err);
        });
    },
    /**
     * 获取指定目录下的文章列表
     */
    getListCategoryId:(req,res,next) =>{
        // 从路由中传递参数进来
        let id = req.params.id;
        Article.getListCategoryId(id,res.start,res.size).then(results =>{
            req.articles = results;
            next()
        }).catch(err=>{
            next(err);
        });
    },
    /**
     * 获取指定关键词的文章列表
     */
    getListBykeywrod:(req,res,next) =>{
        // 获取搜索框的表单数据
        let keyword = req.query.keyword;
        console.log(keyword);
        Article.getListBykeywrod(keyword).then(results =>{
            req.articles = results;
            next();
        }).catch(err=>{
            next(err);
        });
    },
    /**
     * 获取指定文章的详情
    */
   getArticleById:(req,res,next) =>{
        // 从路由中传递参数进来
        let id = req.params.id;
        Article.getArticleById(id).then(results =>{
            req.articles = results;
            if(!req.articles){
                res.render('404');
            }
            next()
        }).catch(err=>{
            next(err);
        });
    },
    /**
     * 获取指定文章的标签列表
    */
   getTabs:(req,res,next) =>{
        // 从路由中传递参数进来
        let id = req.params.id;
        Tab.getListByArticleId(id).then(results =>{
            req.tabs = results;
            next()
        }).catch(err=>{
            next(err);
        });
    },
    /**
     * 获取上一篇文章
    */
    getPrevArticle:(req,res,next) =>{
        // 从路由中传递参数进来
        let id = req.params.id;
        Article.getPrevArticle(id).then(results =>{
            req.prev = results;
            next()
        }).catch(err=>{
            next(err);
        });
    },
    /**
     * 获取下一篇文章
    */
   getNextArticle:(req,res,next) =>{
        // 从路由中传递参数进来
        let id = req.params.id;
        Article.getNextArticle(id).then(results =>{
            req.next = results;
            next()
        }).catch(err=>{
            next(err);
        });
    },
    /**
     *  前台获取总博文数
     */
    getCount:(req,res,next) =>{
        Article.getCount().then(results =>{
            req.articleCount = results;
            next();
        }).catch(err=>{
            next(err);
        });
    },
    /**
     * 前台获取指定页的文章列表
    */
   getPage:(req,res,next) =>{
        Article.getPage(res.start,res.size).then(results =>{
            req.pageList = results;
            next()
        }).catch(err=>{
            next(err);
        });
    },
    /**
     *  后台获取总博文数
     */
    getCountEnd:(req,res,next) =>{
        Article.getCountEnd(req.query.category_id,req.query.hot).then(results =>{
            req.articleCount = results;
            next();
        }).catch(err=>{
            next(err);
        });
    },
    /**
     * 后台指定页的文章列表
    */
   getPageEnd:(req,res,next) =>{
        Article.getPageEnd(res.start,res.size,req.query.category_id,req.query.hot).then(results =>{
            req.pageList = results;
            next()
        }).catch(err=>{
            next(err);
        });
    },
    /**
     * 设置热门推荐
     **/
    setHot:(req,res,next) =>{
        let {id,hot} = req.query;
        Article.setHot(id,hot).then(results =>{
            req.affectedRows = results;
            next()
        }).catch(err=>{
            next(err);
        });
    },
    /**
     * 添加文章
     * @param {Object} article 添加的对象
    */
    add:(req,res,next) =>{
        let {title,content,hot,category_id} = req.body;
        console.log(req.body)
        console.log(title,content,hot,category_id)
        let article = {title,content,category_id,
            hot: hot? 1:0 ,
            thumbnail : req.uploadUrl ? req.uploadUrl :null
        };
        Article.add(article).then(results =>{
            req.insertId = results;
            next()
        }).catch(err=>{
            next(err);
        });
    },
    /**
     * 删除文章
     **/
    del:(req,res,next) =>{
        let {id} = req.query;
        Article.del(id).then(results =>{
            req.affectedRows = results;
            next()
        }).catch(err=>{
            next(err);
        });
    },
    /**
     * 编辑文章
    **/
    edit:(req,res,next) =>{
        let {title,content,hot,category_id,thumbnail,id} = req.body;
        console.log(req.body)
        let article = {title,content,category_id,id,
            hot,
            thumbnail:req.uploadUrl ? req.uploadUrl :thumbnail
        };
        Article.edit(article).then(results =>{
            req.affectedRows = results;
            next()
        }).catch(err=>{
            next(err);
        });
    },
}